<?php
  /**
   * iJConfigControl
   *
   * The iJConfig class :description
   *
   * @copyright Copyright (c) 2011, iJBloks Progect
   */
    class ConfigControl extends iJBlock {
        
        /**
         *
         * Class Constructuctor
         * @access public
         */
        public function __construct() {
    
        }
        
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJConfig    = iJFactory::singleton('iJConfig');
          $iJLanguages = iJFactory::singleton('iJLanguages');
          
          if(isset($_POST['key']) && isset($_POST['value'])) {
            if(iJValidation::name($_POST['key']) && iJValidation::name($_POST['value'], 1)){
               $state = $iJConfig->add($_POST['key'], $_POST['value']);
               if($state) {
                 iJMessages::display('success', 'applayed');
               } else {
                 iJMessages::display('fail', 'fail');
               }
            } else {
              iJMessages::display('fail', 'incorrect_data');
            }
          }
          
          if(isset($_GET['delKey']) && iJValidation::name($_GET['delKey'])) {
             $state = $iJConfig->remove($_GET['delKey']);
             if($state) {
               iJMessages::display('success', 'deleted');
             } else {
                
               iJMessages::display('warning', 'warning', 'bad_key');
             }
          } else {
            
          }
          
          //Deleting from file
          
          if(isset($_GET['del']) && iJValidation::name($_GET['del'])) {
            if (!$iJConfig->delVar($_GET['del'],CONFIG_PATH)) {
               iJMessages::display('warning', 'warning', 'bad_key');
            } else {
               iJMessages::display('success', 'deleted');
            }
            
          }
          
          $page   = isset($_REQUEST['page'])                                  ? $_REQUEST['page']   : 1;
          $search = isset($_REQUEST['search']) && !empty($_REQUEST['search']) ? $_REQUEST['search'] : '';
          
          $fconf       = $iJConfig->getFileConfig($search);
          $blocksList  = $iJConfig->getConfigList($page, $search);
          $conf        = $blocksList[0];
          $QConfig     = $blocksList[1];
          
          $pageArray = array();

          $index = RESULT_LIMIT * ($page-1);
          
          for($i = 0; $i < RESULT_LIMIT; $i++) {
            if(!isset($fconf[$index + $i])) {
               break;
            }
            $pageArray[$i] = $fconf[$index + $i];
            $pageArray[$i]['trash'] = iJHrefLink::linkToLocalPage('admin_config') . '?del=' . $pageArray[$i]['name'];
          }
          
          
          if($page <= 1) {
            $this->addToken('fprev_link', iJHrefLink::linkToLocalPage('', iJHrefLink::Icon('previous_disabled', '32x32')));
          } else {
            $get['page'] = $page - 1;
            $this->addToken('fprev_link', iJHrefLink::linkToLocalPage('admin_config', iJHrefLink::Icon('previous', '32x32'), $get));
          }
          
          if($page >= ceil(count($fconf) / RESULT_LIMIT)) {
            $this->addToken('fnext_link', iJHrefLink::linkToLocalPage('', iJHrefLink::Icon('next_disabled', '32x32')));
          } else {
            $get['page'] = $page + 1;
            $this->addToken('fnext_link',  iJHrefLink::linkToLocalPage('admin_config', iJHrefLink::Icon('next', '32x32'), $get));
          }
          
          $loc['loc']  = 2;
          $this->addToken('dbconf',      $conf);         
          $this->addToken('search',      $search);
          $this->addToken('page',        $page);
          $this->addToken('fconf',       $pageArray);

          $this->addToken('delete_icon',   iJHrefLink::Icon('trash',     '16x16'));
          $this->addToken('config_icon',   iJHrefLink::Icon('configure', '16x16'));
          $this->addToken('edit_icon',     iJHrefLink::Icon('edit', '16x16'));
          
          $this->addToken('form_action', iJHrefLink::linkToLocalPage('admin_config') . '?' . iJGetAllGetParams(array('delKey'))); 
          $this->addToken('curLanguage', $iJLanguages->getLanguage());
          $this->addToken('languages',   $iJLanguages->getLanguagesExeptCurrent());  
          
          $this->addToken('total_items',    $QConfig->getSize());
          $this->addToken('ftotal_items',   count($fconf));
          $this->addToken('fpages_count',   ceil(count($fconf) / RESULT_LIMIT));
          $this->addToken('pages_count',    $QConfig->getPagesCount());
          $this->addToken('prev_link',      $QConfig->getPerviousLink($page, 'admin_config', $loc));
          $this->addToken('next_link',      $QConfig->getNextLink($page,     'admin_config', $loc)); 
          return parent::execute();
        }
        
    }
?>